package controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import model.PluginPath;

public class PlateformeMain {

	/**
	 * @param args
	 */
	
	public static final PlateformeCore core = new PlateformeCore();
	
	private static void getPaths(){
		//recuperation des path
		File thisFolder = new File("./");
		for(int i=0;i<thisFolder.list().length;i++){
			if(thisFolder.listFiles()[i].getName().equals("plugins_path.ser")){
				 // ouverture d'un flux sur un fichier
				ObjectInputStream ois = null;
				try {
					ois = new ObjectInputStream(new FileInputStream(thisFolder.listFiles()[i]));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 // désérialization de l'objet
				try {
					core.getPluginStorage().setPluginPath((PluginPath)ois.readObject());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(new File("./plugins_path.ser")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		PluginPath pp = new PluginPath();
		try {
			oos.writeObject(pp) ;
		} catch (IOException e) {
			e.printStackTrace();
		}
		core.getPluginStorage().setPluginPath(pp);
	}
	
	public static void main(String[] args) {

		//recupere chemin dossier des fichiers .cfg plugins
		getPaths();
		
		//lancement du coeur de l'application
		core.launch();
		
		//lancement de l'interface graphique
		core.loadPlugin("InterfaceGraphiquePlugin");
	}
}
